<template>
	<view class="content">
		<view class="gz-list">
			<view class="block">
				<view class="title">商户信息</view>
				<view class="gz-list-item">
					<view class="gz-list-left">
						<view class="title"><text>商户号</text></view>
					</view>
					<view class="gz-list-right">
						<input type="text" placeholder="请输入商户号" v-model="vdata.saveObject.mchNo" />
					</view>
				</view>
				<view class="gz-list-item">
					<view class="gz-list-left">
						<view class="title"><text>手机号</text></view>
					</view>
					<view class="gz-list-right">
						<input type="tel" maxlength="11" placeholder="请输入手机号" v-model="vdata.saveObject.iLoginUsername" />
					</view>
				</view>
				<view class="gz-list-item gz-vc-item">
					<view class="gz-list-left">
						<view class="title"><text>验证码</text></view>
					</view>
					<view class="gz-list-right">
						<input type="number" placeholder="请输入验证码" v-model="vdata.saveObject.vc" />
						<button @click="getMchSmsCode" plain :disabled="vcConfig.disabled" :style="{ color: vcConfig.color, border: `2rpx solid ${vcConfig.color}` }">{{ vcConfig.text }}</button>
					</view>
				</view>
			</view>
		</view>

		<view class="gz-button" @tap="save"><text>认证商户</text></view>
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app'
import { reactive, computed, ref, defineProps, onUnmounted, getCurrentInstance } from 'vue'
import appConfig from '@/config/appConfig';
import storageManage from '@/util/storageManage.js'
import { $sendSmsCode, $bindMchCc } from '@/http/apiManager.js'
import { checkPhone } from '@/util/validate.js'

const { $infoBox } = getCurrentInstance().appContext.config.globalProperties

// 验证码相关配置
const vcConfig = reactive({
	timer: null,	// 定时器
	text: '获取验证码',
	color: '#24c789',
	disabled: false,
})
onUnmounted(()=>{
	// 页面卸载 清空定时器
	if(vcConfig.timer){
		clearInterval(vcConfig.timer)
	}
})
/**
 * 获取验证码 重置
 */
function originCode(){
	if(vcConfig.timer){
		clearInterval(vcConfig.timer)
	}
	vcConfig.text = '获取验证码'
	vcConfig.color = '#24c789'
	vcConfig.disabled = false
}
/**
 * 获取验证码
 */
function getMchSmsCode(){
	// 校验用户手机号
	if(!checkPhone(vdata.saveObject.iLoginUsername)){
		$infoBox.toast('请输入正确的手机号')
		return
	}
	// 发送验证码
	vcConfig.disabled = true
	vcConfig.text = '发送中...'
	vcConfig.color = '#999'
	$sendSmsCode({ phone: vdata.saveObject.iLoginUsername,flag:8 }).then(res => {
		vcConfig.text = "重新获取(60)"
		let holdTime = 59; //定义变量并赋值
		vcConfig.timer = setInterval(() => {
			if (holdTime <= 0) {
				originCode()
				return; //返回前面
			}
			vcConfig.text = "重新获取(" + holdTime + ")"
			holdTime--;
		}, 1000)
	}).catch(()=>{
		originCode()
	})
}

const vdata = reactive({
	saveObject: { // 提交数据
		iLoginUsername: '',
		mchNo: '',
		vc: '',
	},
})

// 提交保存--------------------------------------
const save = () => {
	getApp().preventActive(()=>{
		// 信息校验
		const { mchNo, iLoginUsername, vc } = vdata.saveObject
		// 校验商户号
		if(!mchNo){
			$infoBox.toast('请输入商户号')
			return
		}
		// 校验手机号
		if(!checkPhone(vdata.saveObject.iLoginUsername)){
			$infoBox.toast('请输入正确的手机号')
			return
		}
		// 校验验证码
		if(!vc){
			$infoBox.toast('请输入验证码')
			return
		}
		uni.showLoading({
			title: '绑定中...',
			mask: true
		})
		$bindMchCc({ phone: iLoginUsername, code: vc, mchNo }).then(({ bizData }) => {
			// 本地缓存商户信息
			storageManage.MCHS_CC(bizData)
			$infoBox.toast('绑定成功',{ duration: 1500, mask: true })
			setTimeout(() => {	// 绑定成功后，关闭当前页面跳转到商家工具
				uni.redirectTo({
					url: '/pageUser/bindMchCc/mchTool'
				})
			},1500)
		})
	})
}
</script>

<style lang="scss" scoped>
.content {
	width: 100%;
	min-height: 100vh;
	background: #F7F8FB;
	box-sizing: border-box;
	overflow: hidden;

	.store-list {
		width: 100%;
		height: 100%;
	}

	.gz-list {
		padding: 24rpx 24rpx 0;
		box-sizing: border-box;

		&::after,
		&::before {
			content: '';
			width: 0;
			height: 0;
			background-color: transparent;
		}


		.gz-list-item {

			align-items: center;
			padding: 30rpx;

			&::after {
				content: '';
				width: 0;
				height: 0;
				background-color: transparent;
			}

			.gz-list-left {
				flex-shrink: 0;
				.title {
					text {
						font-size: 32rpx;
						color: #333333;
					}
				}

				.number {
					text {
						font-size: 22rpx;
						color: #aaa;
					}
				}
			}

			.gz-list-right {
				display: flex;
				align-items: center;
				gap: 20rpx;

				text {
					font-weight: 400;
					font-size: 28rpx;
					color: #666666;
				}

				image {
					width: 14rpx !important;
					height: 24rpx !important;
					margin-left: 20rpx;
				}

				.switch {
					width: 50rpx;
					height: 100%;
					margin-left: 10rpx;
				}

				button {
					height: 50rpx;
					border-radius: 25rpx;
					display: inline-block;
					line-height: 50rpx;
					font-size: 12px;
					padding: 0;
					width: 220rpx;
					margin-left: 20rpx;
				}

				input {
					width: 100%;
					text-align: right;
					font-size: 28rpx;
					color: #333;
				}

				image {
					margin-left: 10rpx;
					width: 50rpx;
					height: 50rpx;
				}

				.number-text {
					color: #9ea5b3;
				}
			}
		}

		.gz-vc-item{
			.gz-list-right{
				button{
					width: initial;
					border: none;
					font-size: 28rpx;
					background-color: #fff;
					flex-shrink: 0;
					border-radius: 8rpx;
					padding: 0 15rpx;
				}
			}
		}

		.block {
			border-radius: 20rpx;
			overflow: hidden;
			background-color: #FFFFFF;
			margin-bottom: 24rpx;
			position: relative;
			
			&>.title{
				padding: 32rpx 0 0 32rpx;
				font-size: 36rpx;
				font-weight: bold;
			}

			.gz-list-item {
				position: relative;

				&::after {
					content: '';
					display: block;
					position: absolute;
					bottom: 0;
					width: calc(100% - 60rpx);
					height: 2rpx;
					background-color: #f0f0f0;
				}
			}
		}

		.switch-item {
			padding: 0 30rpx;
			align-items: center;
			height: 110rpx;
		}
	}

	.gz-button {
		width: calc(100% - 48rpx);
		height: 90rpx;
		height: 100rpx;
		background: #24c789;
		border-radius: 50rpx;
		border: 2rpx solid #DADADA;
		border-radius: 50rpx;
		margin: 80rpx auto 0;

		display: flex;
		justify-content: center;
		align-items: center;
		font-weight: 500;
		font-size: 32rpx;
		color: #fff;
	}
}

.swiper-item {
	display: flex;
	justify-content: space-between;
	flex-direction: column;
	width: 600rpx;
	height: 320rpx;
	background: #fff;
	border-radius: 20rpx;
	overflow: hidden;
	box-sizing: border-box;
	padding: 20rpx;

	.title {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin: 30rpx auto 50rpx auto;

		text {
			font-size: 26rpx;
			color: #3981FF;
		}

		image {
			margin-right: 20rpx;
			width: 30rpx;
			height: 30rpx;
		}

		.money {
			text {
				font-size: 33rpx;
				font-weight: 400;
				color: #000;
			}
		}
	}

	.buttons {
		display: flex;
		justify-content: space-between;

		.cancel,
		.confirm {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 270rpx;
			height: 90rpx;
			border-radius: 10rpx;

			text {
				font-size: 30rpx;
			}
		}

		.cancel {
			border: 2rpx solid #c5c7cc;

			text {
				color: #808080;
			}
		}

		.confirm {
			background: #3981FF;
			color: #fff;
		}
	}
}

.font {
	font-family: "PingFang SC Medium";
	font-weight: 500;
	font-size: 28rpx;
	text-align: left;
	color: #000;
}

:deep(.input-arrow) {
	display: none !important;
}

:deep(.input-value-border) {
	border: none !important;
	height: initial !important;
}

:deep(.input-placeholder) {
	font-size: 28rpx !important;
	color: #666 !important;
}

.uni-input {
	font-size: 24rpx;
	color: #666;
}
</style>